// dnSpy decompiler from Assembly-CSharp.dll class: XMLSerialization
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using UnityEngine;

public class XMLSerialization
{
	public static string UTF8ByteArrayToString(byte[] characters)
	{
		UTF8Encoding utf8Encoding = new UTF8Encoding();
		return utf8Encoding.GetString(characters);
	}

	public static byte[] StringToUTF8ByteArray(string pXmlString)
	{
		UTF8Encoding utf8Encoding = new UTF8Encoding();
		return utf8Encoding.GetBytes(pXmlString);
	}

	public static string SerializeObject<T>(object pObject)
	{
		MemoryStream memoryStream = new MemoryStream();
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
		xmlSerializer.Serialize(xmlTextWriter, pObject);
		memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
		return XMLSerialization.UTF8ByteArrayToString(memoryStream.ToArray());
	}

	public static T DeserializeObject<T>(string pXmlizedString) where T : class
	{
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
		MemoryStream stream = new MemoryStream(XMLSerialization.StringToUTF8ByteArray(pXmlizedString));
		return xmlSerializer.Deserialize(stream) as T;
	}

	public static void CreateXML(string sPath, string _data)
	{
		FileInfo fileInfo = new FileInfo(sPath);
		StreamWriter streamWriter;
		if (!fileInfo.Exists)
		{
			streamWriter = fileInfo.CreateText();
		}
		else
		{
			fileInfo.Delete();
			streamWriter = fileInfo.CreateText();
		}
		streamWriter.Write(_data);
		streamWriter.Close();
	}

	public static string LoadXML(string sPath)
	{
		StreamReader streamReader = File.OpenText(sPath);
		string result = streamReader.ReadToEnd();
		streamReader.Close();
		return result;
	}

	public static void SaveObjectToCacheFile<T>(T obj, string fileName)
	{
		string data = XMLSerialization.SerializeObject<T>(obj);
		XMLSerialization.CreateXML(Application.temporaryCachePath + "/" + fileName, data);
	}

	public static T LoadObjectFromCacheFile<T>(string fileName) where T : class
	{
		string pXmlizedString = XMLSerialization.LoadXML(Application.temporaryCachePath + "/" + fileName);
		return XMLSerialization.DeserializeObject<T>(pXmlizedString);
	}

	public static void DeleteFileFromCache(string fileName)
	{
		File.Delete(Application.temporaryCachePath + "/" + fileName);
	}

	public static bool FileExistsInCache(string fileName)
	{
		return File.Exists(Application.temporaryCachePath + "/" + fileName);
	}

	public static void SaveObjectToPersistentFile<T>(T obj, string fileName)
	{
		string data = XMLSerialization.SerializeObject<T>(obj);
		XMLSerialization.CreateXML(Application.persistentDataPath + "/" + fileName, data);
	}

	public static T LoadObjectFromPersistentFile<T>(string fileName) where T : class
	{
		string pXmlizedString = XMLSerialization.LoadXML(Application.persistentDataPath + "/" + fileName);
		return XMLSerialization.DeserializeObject<T>(pXmlizedString);
	}

	public static void DeleteFileFromPersistentStorage(string fileName)
	{
		File.Delete(Application.persistentDataPath + "/" + fileName);
	}

	public static bool FileExistsInPersistentStorage(string fileName)
	{
		return File.Exists(Application.persistentDataPath + "/" + fileName);
	}
}
